package com.bilibili.server.controller;

import com.bilibili.server.dto.Response;
import com.bilibili.server.entity.VideoType;
import com.bilibili.server.service.VideoTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/video-types")
public class VideoTypeController {
    @Autowired
    private VideoTypeService videoTypeService;

    // 获取所有视频类型（公开接口）
    @GetMapping
    public ResponseEntity<Response<List<VideoType>>> getAllVideoTypes() {
        try {
            List<VideoType> videoTypes = videoTypeService.getAllVideoTypes();
            return new ResponseEntity<>(Response.success("获取成功", videoTypes), HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity<>(Response.error(500, "服务器内部错误"), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    // 根据ID获取视频类型详情（公开接口）
    @GetMapping("/{typeId}")
    public ResponseEntity<Response<VideoType>> getVideoTypeById(@PathVariable int typeId) {
        try {
            VideoType videoType = videoTypeService.getVideoTypeById(typeId);
            if (videoType == null) {
                return new ResponseEntity<>(Response.error(404, "视频类型不存在"), HttpStatus.NOT_FOUND);
            }
            return new ResponseEntity<>(Response.success("获取成功", videoType), HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity<>(Response.error(500, "服务器内部错误"), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    // 创建新视频类型（需要admin权限）
    @PostMapping
    @PreAuthorize("hasRole('ADMIN')")
    public ResponseEntity<Response<VideoType>> createVideoType(@RequestBody VideoType videoType) {
        try {
            VideoType newVideoType = videoTypeService.createVideoType(videoType);
            return new ResponseEntity<>(Response.created("创建成功", newVideoType), HttpStatus.CREATED);
        } catch (RuntimeException e) {
            return new ResponseEntity<>(Response.error(400, e.getMessage()), HttpStatus.BAD_REQUEST);
        } catch (Exception e) {
            return new ResponseEntity<>(Response.error(500, "服务器内部错误"), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    // 更新视频类型（需要admin权限）
    @PutMapping("/{typeId}")
    @PreAuthorize("hasRole('ADMIN')")
    public ResponseEntity<Response<VideoType>> updateVideoType(@PathVariable int typeId, @RequestBody VideoType videoType) {
        try {
            VideoType updatedVideoType = videoTypeService.updateVideoType(typeId, videoType);
            if (updatedVideoType == null) {
                return new ResponseEntity<>(Response.error(404, "视频类型不存在"), HttpStatus.NOT_FOUND);
            }
            return new ResponseEntity<>(Response.success("更新成功", updatedVideoType), HttpStatus.OK);
        } catch (RuntimeException e) {
            return new ResponseEntity<>(Response.error(400, e.getMessage()), HttpStatus.BAD_REQUEST);
        } catch (Exception e) {
            return new ResponseEntity<>(Response.error(500, "服务器内部错误"), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    // 删除视频类型（需要admin权限）
    @DeleteMapping("/{typeId}")
    @PreAuthorize("hasRole('ADMIN')")
    public ResponseEntity<Response<Void>> deleteVideoType(@PathVariable int typeId) {
        try {
            videoTypeService.deleteVideoType(typeId);
            return new ResponseEntity<>(Response.success("删除成功"), HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity<>(Response.error(500, "服务器内部错误"), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
}